﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DnaCoreLib;

namespace DnaServiceLib
{
    public class Account:IAccount
    {
        public Account()
        {
        }

        public Account(Guid id, string username)
        {
            Id = id;
            Username = username;
        }
        public Guid Id { get; set; }
        public string Username { get; set; }

        public override string ToString()
        {
            return String.Format("{0}", Username);
        }

        public IEnumerable<DnaPerson> DnaPersonList
        {
            get
            {
                IEnumerable<DnaPerson> dnaPeople = new List<DnaPerson>();
                using (DnaDataLib.DnaGedDataContext DnaDc = new DnaDataLib.DnaGedDataContext())
                {
                    dnaPeople = (from p in DnaDc.DnaPersons
                                 join l in DnaDc.Link_Account_DnaPersons
                                 on p.Id equals l.DnaPersonId
                                 where l.AccountId == Id
                                 select new DnaPerson()
                                 {
                                     Id = p.Id,
                                     FirstName = p.FirstName,
                                     LastName = p.LastName,
                                     Gender = p.Gender,
                                     FatherDnaPersonId = p.FatherDnaPersonId,
                                     MotherDnaPersonId = p.MotherDnaPersonId,
                                 });

                    return dnaPeople.ToArray();
                }
            }

        }

        public void LinkToExistingDnaPerson(Guid personGuid, byte accessLevel)
        {
            using (DnaDataLib.DnaGedDataContext DnaDc = new DnaDataLib.DnaGedDataContext())
            {
                DnaDataLib.Link_Account_DnaPerson newLink = new DnaDataLib.Link_Account_DnaPerson()
                {
                    AccountId = Id,
                    DnaPersonId = personGuid,
                    AccessLevel = accessLevel,
                    ManagedByAccountId = Id
                };
                DnaDc.Link_Account_DnaPersons.InsertOnSubmit(newLink);
                DnaDc.SubmitChanges();
            }

        }

        public void LinkToNewDnaPerson(DnaPerson dnaPerson)
        {
            string firstName=dnaPerson.FirstName;
            string lastName = dnaPerson.LastName;
            char? gender=dnaPerson.Gender;
            Guid? fatherDnaPersonId = dnaPerson.FatherDnaPersonId;
            Guid? motherDnaPersonId = dnaPerson.MotherDnaPersonId;

            using (DnaDataLib.DnaGedDataContext DnaDc=new DnaDataLib.DnaGedDataContext())
            {
                DnaDataLib.DnaPerson newDnaPerson = new DnaDataLib.DnaPerson()
                {
                    Id = Guid.NewGuid(),
                    FirstName = firstName,
                    LastName = lastName,
                    Gender = gender,
                    FatherDnaPersonId = fatherDnaPersonId,
                    MotherDnaPersonId = motherDnaPersonId
                };
                DnaDc.DnaPersons.InsertOnSubmit(newDnaPerson);

                DnaDataLib.Link_Account_DnaPerson newLink = new DnaDataLib.Link_Account_DnaPerson()
                {
                    Id = Guid.NewGuid(),
                    AccountId = Id,
                    DnaPerson = newDnaPerson,
                    AccessLevel = 1,
                    ManagedByAccountId = Id
                };
                DnaDc.Link_Account_DnaPersons.InsertOnSubmit(newLink);
                DnaDc.SubmitChanges();


            }
        }

        public void DeleteDnaPerson(DnaPerson dnaPerson)
        {
            using (DnaDataLib.DnaGedDataContext DnaDc = new DnaDataLib.DnaGedDataContext())
            {
                var deleteAccountPersonLinks = DnaDc.Link_Account_DnaPersons.Where(w => w.DnaPersonId == dnaPerson.Id);
                DnaDc.Link_Account_DnaPersons.DeleteAllOnSubmit(deleteAccountPersonLinks);

                var deletePerson = DnaDc.DnaPersons.Where(w=>w.Id==dnaPerson.Id).FirstOrDefault();
                DnaDc.DnaPersons.DeleteOnSubmit(deletePerson);
                DnaDc.SubmitChanges();
            }
        }

        public bool SaveChanges()
        {
            using (DnaDataLib.DnaGedDataContext DnaDc = new DnaDataLib.DnaGedDataContext())
            {
                DnaDataLib.Account account = DnaDc.Accounts.Where(w => w.Id == Id).FirstOrDefault();
                if (account != null)
                {
                    account.Username = Username;
                    DnaDc.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }
    }
}
